﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SX.Utils
{
    public static class ConfigHelper
    {
        private static ConcurrentDictionary<string, string> appConfigs = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        private static ConcurrentDictionary<string, string> connConfigs = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        /// <summary>
        /// 获取Web.config或App.config的值（允许值不存在或为空时输出默认值）。
        /// </summary>
        public static string GetApp(string key)
        {
            return GetApp(key, "");
        }
        
        /// <summary>
        /// 获取Web.config或App.config的值（允许值不存在或为空时输出默认值）。
        /// </summary>
        public static string GetApp(string key, string defaultValue)
        {
            string result;
            if (appConfigs.TryGetValue(key, out result))
            {
                return result;
            }
            else
            {
                string value = ConfigurationManager.AppSettings[key];
                value = string.IsNullOrEmpty(value) ? defaultValue : value;
                appConfigs[key] = value;
                return value;
            }
        }
        
        /// <summary>
        /// 获取Web.config或App.config的connectionStrings节点的值。
        /// </summary>
        public static string GetConn(string name, out string providerName)
        {
            providerName = string.Empty;
            if (string.IsNullOrEmpty(name))
            {
                name = "Conn";
            }
            if (name.Trim().Contains(" "))
            {
                return name;
            }
            if (connConfigs.ContainsKey(name))
            {
                return connConfigs[name];
            }
            ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings[name];
            if (conn != null)
            {
                providerName = conn.ProviderName;
               
                return conn.ConnectionString;
            } 
            return "";
        }

        public static bool GetAppBool(string key, bool defaultValue)
        {
            var value = GetApp(key);
            if (value.IsNullOrEmpty())
            {
                return defaultValue;
            }
            else
            {
                return CommonHelper.ToBool(value, defaultValue);
            }
        }
        public static int GetAppInt(string key, int defaultValue = 0)
        {
            var value = GetApp(key);
            if (value.IsNullOrEmpty())
            {
                return defaultValue;
            }
            else
            {
                return CommonHelper.ToInt(value, defaultValue);
            }
        }
        /// <summary>
        /// 获取Web.config或App.config的connectionStrings节点的值。
        /// </summary>
        public static string GetConn(string name)
        {
            string p;
            return GetConn(name, out p);
        }
        /// <summary>
        /// 启动是否清除插件目录
        /// </summary>
        public static bool ClearModuleShadowDirectoryOnStartup{
            get { 
                return GetAppBool("ClearModuleShadowDirectoryOnStartup",true);
            }
        }
    }
}
